/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：CommandCopy.js<br>
 * 日期：2018年9月12日<br>
 * 作者: 孙燕宝<br>
 * 功能：显示图片命令<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.CommandShowAllImg<br>
 * 类型功能：显示图片命令<br>
 */
Rainier.UI.V2.CommandShowAllImg = draw2d.command.Command.extend({
	
	NAME : "Rainier.UI.V2.CommandShowAllImg",
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandShowAllImg.init<br>
	 * 函数功能：构造函数<br>
	 * 异常信息：无<br>
	 */
	init : function(context,editParts){
		this.context 		= context;
    	this.rootEditPart 	= this.context.getRootEditPart();
    	// 除了已经隐藏的图片模型的图片模型
    	this.showImgArr = [];
    	this.tabId = app.canvasId;
		this._super();
	},
	
    /**
	 * 函数名称：Rainier.UI.V2.CommandShowAllImg.execute<br>
	 * 函数功能：执行命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    execute:function(){
    	var _this = this;
    	if(!hideImgArr[this.tabId]){
    		return;
    	}
    	if(hideImgArr[this.tabId].length>0){
    		$.each(hideImgArr[_this.tabId],function(index,deleteInfo){
				var currentModel 	 = deleteInfo.model;
				var connInfoStack	 = deleteInfo.deleteConnInfoStack.clone();
				if(currentModel instanceof Rainier.UI.V2.DeviceModel){
					var portList 	= deleteInfo.ports.clone();
					portList.each(function(index,port){
						currentModel.addPort(port);
					});

					var parentModel = currentModel.getParent();

					// 撤销时，修改自定义器件的图片
					currentModel.property.imageProperty.svg = currentModel.property.uiProperty.imageString;
					
					parentModel.addChildModel(currentModel);
					var uiProperty = currentModel.getProperty().uiProperty;
					currentModel.setPosition(uiProperty.x,uiProperty.y,false);
				}
				
				//翻转数组，逆序恢复数组中的内容
				connInfoStack.reverse();
				
				//恢复连线：
				connInfoStack.each(function(index,connModelInfo){
					_this.addConnectionModel(connModelInfo);
				})
				_this.rootEditPart.refreshChildren();
				_this.showImgArr.push(deleteInfo);
    		});
    		hideImgArr[this.tabId] = [];
    	}
    	//更新预览图
    	// if(isSelectedPreview){
    	// 	setTimeout(function(){
		// 		var img = $("#img-preview");
		// 		refreshPreview(img);
		// 	});
    	// }
    },
    
    /**
	 * 函数名称：Rainier.UI.V2.CommandShowAllImg.undo<br>
	 * 函数功能：撤销命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    undo:function(){
  		var _this = this;
  		if(_this.showImgArr.length>0){
  			$.each(_this.showImgArr,function(index,deleteInfo){
  				var model = deleteInfo.model;
  				var parentModel = model.getParent();
				parentModel.removeChildModel(model);
				hideImgArr[_this.tabId].push(deleteInfo);
  			});
  			this.showImgArr = [];
  		}
    },
    
    /**
	 * 函数名称：Rainier.UI.V2.CommandShowAllImg.redo<br>
	 * 函数功能：重做命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    redo:function(){
    	this.execute();
    },

     /**
	 * 函数名称：draw2d.command.CommandShowAllImg.addConnectionModel<br>
	 * 函数功能：恢复连线信息的添加<br>
	 * 参数输入：connModelInfo:封装好的connModelInfo信息<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	addConnectionModel : function(connModelInfo){
		var connType	= connModelInfo.connType;
		var connObj 	= connModelInfo.connModel;
		var sourceModel = connModelInfo.sourceModel;
		var targetModel = connModelInfo.targetModel;
		
		//第一步：把当前连线添加进来
		var conn 	   = connObj.connection;

		//第二步：恢复连线两端的jointModel
		if(sourceModel){
			var jointModel 		 = sourceModel.jointModel;
			var hybridPort		 = sourceModel.hybirdPort;
			
			var jointModelId	 = jointModel.getUuid();
			
			var jointModelParent  = jointModel.getParent();
			var connJointModelIds = [];
			var connJointModels   = jointModelParent.getChildren();
			connJointModels.forEach(function(connJointModel,index,connJointModels){
				connJointModelIds.push(connJointModel.getUuid());
			})
			//不存在当前model
			if(connJointModelIds.indexOf(jointModelId) === -1){
				jointModel.addPort(hybridPort);
				jointModelParent.addChildModel(jointModel);    					
			}
		}
		
		if(targetModel){
			var jointModel 		 = targetModel.jointModel;
			var hybridPort		 = targetModel.hybirdPort;
			
			var jointModelId	 = jointModel.getUuid();
			
			var jointModelParent  = jointModel.getParent();
			var connJointModelIds = [];
			var connJointModels   = jointModelParent.getChildren();
			connJointModels.forEach(function(connJointModel,index,connJointModels){
				connJointModelIds.push(connJointModel.getUuid());
			})
			//不存在当前model
			if(connJointModelIds.indexOf(jointModelId) === -1){
				jointModel.addPort(hybridPort);
				jointModelParent.addChildModel(jointModel);    					
			}
		}
		
		//第三步：恢复连线和sourcePort和targetPort的关联关系。
		var sourcePort = connObj.sourcePort;
		var targetPort = connObj.targetPort;
		conn.attachSourcePort(sourcePort);
		conn.attachTargetPort(targetPort);
		sourcePort.attachOutgoing(conn);
		targetPort.attachIncoming(conn);
			
		var connParent = conn.getParent();
		connParent.addConnection(conn);
	},

});